package com.hys.controller;


import com.hys.common.Result;
import com.hys.entity.User;
import com.hys.param.UserUpdateParam;
import com.hys.service.UserService;
import com.hys.util.BeanUtil;
import com.hys.vo.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * 用户信息
 *
 * @author cheer
 */
@RestController
@Api(tags = "用户")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${auth.log-success-url}")
    private String authAppId;

    @GetMapping("login")
    @ApiOperation(value = "登陆 ❌")
    public Result doLogin(String username, String password) {
        userService.doLogin(username, password);
        return Result.success();
    }

    @GetMapping("update")
    @ApiOperation(value = "更新 ❌")
    public Result update(@RequestBody UserUpdateParam userUpdateParam) {
        userService.update();
        return Result.success();
    }

    @GetMapping("switchRole")
    @ApiOperation(value = "登陆用户切换")
    public RedirectView switchLogin(String userName) {
        userService.switchRole(userName);
        return new RedirectView(authAppId);
    }

    @GetMapping("login/token")
    @ApiOperation(value = "TODO token登陆",notes = "与第三方系统对接")
    public RedirectView loginByToken(String appkey) {
        userService.doLogin(appkey);
        return new RedirectView(authAppId);
    }

    @GetMapping("info")
    @ApiOperation(value = "TODO 用户信息",notes = "role: admin, dept-admin <增删改查 权限 无审核> <br/>" +
            " role: audit <查看，审核按钮权限 无增删改权限> ")
    public Result<UserInfoVO> info() {
        User user = userService.userInfo();
        UserInfoVO userVO = BeanUtil.transform(UserInfoVO.class, user);
        return Result.success(userVO);
    }


    @GetMapping("logout")
    @ApiOperation(value = "登出 ❌")
    public Result logout() {
        userService.logout();
        return Result.success();
    }

}
